<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'srm/puorder:chg:puOrderChgLog:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ orderId: record.orderId })">
          {{ record.orderCode }}
        </a>
      </template> -->
      <template #checkBox="{ record, column }">
        
        <Checkbox :checked="record[column.dataIndex] == '1' ? true : false" />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsSrmPuorderChgPuOrderChgLogList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { puOrderChgLogDelete, puOrderChgLogListData } from '/@/api/srm/puorder/chg/puOrderChgLog';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { Checkbox } from 'ant-design-vue';

  const { t } = useI18n('srm/puorder.chg.puOrderChgLog');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('变更确认记录管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('订单号'),
        field: 'orderCode',
        component: 'Input',
      },
      {
        label: t('订单版本'),
        field: 'orderVersion',
        component: 'Input',
      },
      {
        label: t('确认人'),
        field: 'comfirmByName',
        component: 'Input',
      },
      {
        label: t('确认日期'),
        field: 'comfirmDate',
        component: 'RangePicker',
        componentProps: {
          // format: 'YYYY-MM-DD HH:mm',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
    fieldMapToTime: [['comfirmDate', ['comfirmDate_gte', 'comfirmDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单号'),
      dataIndex: 'orderCode',
      key: 'a.order_code',
      sorter: true,
      width: 200,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('订单版本'),
      dataIndex: 'versionDesc',
      key: 'a.order_version',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('确认人'),
      dataIndex: 'comfirmByName',
      key: 'a.comfirm_by_name',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('确认日期'),
      dataIndex: 'comfirmDate',
      key: 'a.comfirm_date',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('是否自动'),
      dataIndex: 'bauto',
      key: 'a.bauto',
      sorter: true,
      width: 80,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否变更'),
      dataIndex: 'bchg',
      key: 'a.bchg',
      sorter: true,
      width: 80,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑变更确认记录'),
        onClick: handleForm.bind(this, { orderId: record.orderId }),
        auth: 'srm/puorder:chg:puOrderChgLog:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: puOrderChgLogListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { orderId: record.orderId };
    const res = await puOrderChgLogDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
